package com.whsxt.buss.controller;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whsxt.buss.data.ProviderData;
import com.whsxt.buss.domain.BusProvider;
import com.whsxt.buss.service.IBusProviderService;
import com.whsxt.common.CodeMsg;
import com.whsxt.common.PageInfo;
import com.whsxt.common.Result;
import com.whsxt.common.util.ConstantUtil;
import cn.hutool.core.util.StrUtil;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 贺炯
 * @since 2019-09-27
 */
@RestController
@RequestMapping("/buss/provider")
public class BusProviderController {
	
	@Autowired
	private IBusProviderService iBusProviderService;
	
	/**
	 * 	供应商列表
	 * @param data
	 * @return
	 */
	@RequestMapping("/list")
	public Object list(ProviderData data) {
		IPage<BusProvider> page = new Page<BusProvider>(data.getPage(),data.getLimit());
		QueryWrapper<BusProvider> queryWrapper = new QueryWrapper<BusProvider>();
		queryWrapper.eq("available", ConstantUtil.AVAILABLE_TRUE);
		queryWrapper.like(StrUtil.isNotBlank(data.getProvidername()), "providername", data.getProvidername());
		queryWrapper.like(StrUtil.isNotBlank(data.getTelephone()), "telephone", data.getTelephone());
		queryWrapper.like(StrUtil.isNotBlank(data.getConnectionperson()), "connectionperson", data.getConnectionperson());
		iBusProviderService.page(page, queryWrapper);
		PageInfo pageInfo = new PageInfo(page.getTotal(), page.getRecords());
		return new Result(pageInfo);
	}
	/**
	 * 	添加供应商
	 * @param data
	 * @return
	 */
	@RequestMapping("/addBusProvider")
	public Object addBusCustomer(BusProvider data) {
		try {
			iBusProviderService.save(data);
			return new Result(CodeMsg.PROVIDER_ADD_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.PROVIDER_ADD_ERROR);
		}
	}
	/**
	 * 	更新供应商
	 * @param data
	 * @return
	 */
	@RequestMapping("/updateBusProvider")
	public Object updateBusCustomer(BusProvider data) {
		try {
			iBusProviderService.updateById(data);
			return new Result(CodeMsg.PROVIDER_UPDATE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.PROVIDER_UPDATE_ERROE);
		}
	}
	/**
	 * 	供应商单个删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteOne")
	public Object deleteOne(Integer id) {
		try {
			iBusProviderService.removeById(id);
			return new Result(CodeMsg.PROVIDER_DELETE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.PROVIDER_DELETE_ERROE);
		}
	}
	
	/**
	 *    供应商批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/batchDelete")
	public Object batchDelete(Integer [] ids) {
		System.out.println(ids);
		Collection<Serializable> idList = new ArrayList<Serializable>();
		for (Integer id : ids) {
			idList.add(id);
		}
		try {
			iBusProviderService.removeByIds(idList);
			return new Result(CodeMsg.PROVIDER_BATCH_DELETE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.PROVIDER_BATCH_DELETE_ERROE);
		}
	}
}

